import { createRouter, createWebHashHistory,RouteRecordRaw} from 'vue-router'
import Layout from '@/Layout/index.vue'
import error404 from '@/views/error-page/404.vue'
import { shallowRef } from 'vue'

//静态路由，不区分权限
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path:'/',
    component:Layout,
    redirect:'/home',
    children:[
      {
        path:'home',
        component:()=>import('@/views/welcome/index.vue'),
        meta:{
          alwaysShow:false
        }
      }
    ]
  },
  {
    path: '/404',
    component:error404,
    meta: { hidden: true }
  },
]


export const asyncRoutes: Array<RouteRecordRaw> = [
  {
    path:'/users',
    component:shallowRef(Layout),
    redirect:'/users/manage',
    meta:{
      title:'用户管理',
      icon:'user',
      roleLevels:[1],
      alwaysShow:true,
      haveSubs:false
    },
    children:[
      {
        path:'manage',
        component:()=>import('@/views/user-manage/index.vue'),
        meta:{
          title:'用户管理',
          icon:'user',
          roleLevels:[1],
          alwaysShow:true
        }
      }
    ]
  },
  {
    path:'/role',
    component:shallowRef(Layout),
    redirect:'/role/manage',
    meta:{
      title:'角色管理',
      icon:'Stamp',
      roleLevels:[1],
      alwaysShow:true,
      haveSubs:false
    },
    children:[
      {
        path:'manage',
        component:()=>import('@/views/role-manage/index.vue'),
        meta:{
          title:'角色管理',
          icon:'Stamp',
          roleLevels:[1],
          alwaysShow:true
        }
      }
    ]
  },
  {
    path:'/shop',
    component:shallowRef(Layout),
    redirect:'/shop/manage',
    meta:{
      title:'销售信息管理',
      icon:'GoodsFilled',
      roleLevels:[1,2],
      alwaysShow:true,
      haveSubs:true
    },
    children:[
      {
        path:'shopManage',
        component:()=>import('@/views/shop-manage/index.vue'),
        meta:{
          title:'商品管理',
          icon:'User',
          roleLevels:[1,2],
          alwaysShow:true
        }
      },
      {
        path:'shopTypeManage',
        component:()=>import('@/views/shop-type-manage/index.vue'),
        meta:{
          title:'商品类别管理',
          icon:'Goods',
          roleLevels:[1],
          alwaysShow:true
        }
      }
    ]
  },
  {
    path:'/userinfo',
    component:shallowRef(Layout),
    redirect:'/userinfo/manage',
    meta:{
      title:'个人信息',
      icon:'Edit',
      roleLevels:[1],
      alwaysShow:true,
      haveSubs:false
    },
    children:[
      {
        path:'manage',
        component:()=>import('@/views/user-info/index.vue'),
        meta:{
          title:'个人信息',
          icon:'Edit',
          roleLevels:[1],
          alwaysShow:true
        }
      }
    ]
  },

  {
    path: '/:pathMatch(.*)',
    redirect: '/404',
    meta: { alwaysShow: false }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes:constantRoutes
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  console.log("重置路由")
  let newRoutes:RouteRecordRaw[]=[]
  const newRouter = createRouter({history: createWebHashHistory(),routes:newRoutes});
  (router as any).matcher = (newRouter as any).matcher // reset router
}

export default router
